package com.business.action.admin.code;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.admin.PageDefine;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.file.util.FileUtils;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.StringUtil;
import net.sf.rose.web.util.WebUtils;

@Controller
@RequestMapping("/admin/page")
public class PageAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(PageDefine.class);
		Map<String, Object> map = WebUtils.getRequestData(request);
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/get.do")
	public PageBean get(HttpServletRequest request, Service service, String pageID) {
		PageBean page = new PageBean();
        BeanDAO dao = new BeanDAO(service);
        BeanSQL query = dao.getQuerySQL();
        query.setEntityClass(PageDefine.class);
        query.setTableName("SYS_DEF_PAGE_VIEW");
        query.createSql(pageID);
        PageDefine bean = dao.load();
		page.setData(bean);
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, Service service, PageDefine bean) {
		PageBean res = new PageBean();
		if (DBUtils.update(service, bean) > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, Service service, String pageID) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createDeleteSql(PageDefine.class, pageID);
		if(!dao.execute()){
			page.setSuccess(false);
			page.setMessage("表结构信息表删除失败！");
			return page;
		}
		return page;
	}

	/**
	 * 根据模板文件创建html文件
	 */
	@ResponseBody
	@RequestMapping("/create.do")
	public PageBean create(HttpServletRequest request, Service service, String pageID) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.setSQL("select * from SYS_DEF_PAGE where pageID=?");
		query.addParameter(pageID);
		Map<String, Object> map = dao.get();
		String data = map.remove("data").toString();
		map.putAll(StringUtil.parseJson(data));
		// 项目根目录
		File f = WebUtils.WEB_APP_PATH;
		f = new File(f, "page");
		f = new File(f, map.get("savePath").toString());
		f = new File(f, map.get("fileName").toString());
		String templetName = map.get("templetName").toString();
		FileUtils.createFreemakerFile(templetName, map, f);
		System.out.println(map);
		return page;
	}
}
